//思路：利用总和减去前面和得到后面和解决时间复杂度,并累加化为一层循环降低复杂度,注意数据相乘相加类型为long

import java.sql.Array;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String word=in.next();
        char[] ch=word.toCharArray();
        Map<Character,Integer> m=new HashMap<>();
        for (int i = 0; i < ch.length; i++) {
            m.put(ch[i],i);
        }
        int n=in.nextInt();
        String[] s=new String[n];
        for (int i = 0; i <n ; i++) {
            s[i]=in.next();
        }
        Arrays.sort(s,(a,b)->{
            int minlen=Math.min(a.length(),b.length());
            for (int i = 0; i < minlen; i++) {
                int val1=m.get(a.charAt(i));
                int val2=m.get(b.charAt(i));
                int res=val1-val2;
                if(res!=0){
                    return val1-val2;
                }
            }
          return a.length()-b.length();
        });
        for(String s1:s){
            System.out.println(s1);
        }
    }

}













